$(document).ready(function() {
	var userId = $(".userId").text(); //用户ID
	var pagesize = $(".pagesize").text(); //每页记录数
	var contextPath = $(".commonUrl").text(); //绝对路径
	var selectedPageNum = $(".pageNum").val(); //选中页码

	//---------------------------------分页----------------------------------------------

	var pages = $(".pages").text(); //页数
	var pageIndex = $(".pageIndex").text(); //当前页码
	var hasNext = $(".hasNext").text(); //是否有下一页(boolen)
	if (pages == 1) {
		$(".prev,.next").css("cursor", "default");
		$(".prev,.next").css("color", "rgb(166, 166, 166)");
		$(".prev,.next").attr("href", "javascript:void(0)");
	} else {
		if (hasNext == "false") {
			$(".next").css("cursor", "default");
			$(".next").css("color", "rgb(166, 166, 166)");
			$(".next").attr("href", "javascript:void(0)");
		}
		if (pageIndex == 1) {
			$(".prev").css("cursor", "default");
			$(".prev").css("color", "rgb(166, 166, 166)");
			$(".prev").attr("href", "javascript:void(0)");
		}
	}
	for (var i = 1; i < pages;) {
		var result = "";
		i = i + 1;
		result += "<option" + " value=" + "'" + i + "'" + " >" + "第" + i + "页" + "</option>";
		$(".pageNum").append(result);
	}
	$(".pageNum option[value=" + pageIndex + "]").attr("selected", true);

	//----------------------------------异步刷新---------------------------------------------

	$(".pageNum").change(function() {
		var changePageNum = $(".pageNum").val(); //选中页码
		var selfUrl = $(".selfUrl").text(); //请求action
		var ajax_url = contextPath + selfUrl;
		$.ajax({
			url: ajax_url,
			data: "user_id=" + userId + "&pageindex=" + changePageNum + "&pageSize=" + 3,
			success: function(data) {
				var a = eval("(" + data + ")");
				$(".tech-news-grids").empty();
				$.each(a, function(n, commentVal) {
					var result = "";
					result += "<div class='tech-news-grid span_66'>";
					result += commentVal.user_id;
					result += "<p>" + commentVal.content + "</p>";
					result += "</div>";
					$(".tech-news-grids").append(result);
				})
				$(".pageNum option[value=" + pageIndex + "]").attr("selected", true);
			}
		})
		selectedPageNum=changePageNum;
	})
	
	//------------------------------------上一页下一页------------------------------------
	
	$(".prev").click(function() {
					selectedPageNum--;
					$(".pageNum").val(selectedPageNum);  //改变选中的页码
					var selfUrl = $(".selfUrl").text(); //请求action
					var ajax_url = contextPath + selfUrl;
					$.ajax({
						url: ajax_url,
						data: "user_id=" + userId + "&pageindex=" + selectedPageNum + "&pageSize=" + 3,
						success: function(data) {
							var a = eval("(" + data + ")");
							$(".tech-news-grids").empty();
							$.each(a, function(n, commentVal) {
								var result = "";
								result += "<div class='tech-news-grid span_66'>";
								result += commentVal.user_id;
								result += "<p>" + commentVal.content + "</p>";
								result += "</div>";
								$(".tech-news-grids").append(result);
							})
							$(".pageNum option[value=" + pageIndex + "]").attr("selected", true);
						}
					})
					if (0 == selectedPageNum) {
						$(".prev").removeAttr('href');
						$(".prev").addClass('invalid');
					} else {
						$(".prev").attr("href", "javaScript:;");
						$(".prev").removeClass('invalid');
					}
					$(".next").attr("href", "javaScript:;");
					$(".next").removeClass('invalid');
				})
				$(".next").click(function() {
					selectedPageNum++;
					$(".pageNum").val(selectedPageNum);  //改变选中的页码
					var selfUrl = $(".selfUrl").text(); //请求action
					var ajax_url = contextPath + selfUrl;
					$.ajax({
						url: ajax_url,
						data: "user_id=" + userId + "&pageindex=" + selectedPageNum + "&pageSize=" + 3,
						success: function(data) {
							var a = eval("(" + data + ")");
							$(".tech-news-grids").empty();
							$.each(a, function(n, commentVal) {
								var result = "";
								result += "<div class='tech-news-grid span_66'>";
								result += commentVal.user_id;
								result += "<p>" + commentVal.content + "</p>";
								result += "</div>";
								$(".tech-news-grids").append(result);
							})
							$(".pageNum option[value=" + pageIndex + "]").attr("selected", true);
						}
					})
						if (index == (pages-1)) {
							$(".next").removeAttr('href');
							$(".next").addClass('invalid');
						} else {
							$(".next").attr("href", "javaScript:;");
							$(".next").removeClass('invalid');
						}
					$(".prev").attr("href", "javaScript:;");
					$(".prev").removeClass('invalid');
				})

	//-----------------------------------提交评论----------------------------------------

	$(".view").click(function() {
		var commentText = $("#TextArea").val();
		var ajax_url = contextPath;
		if (commentText == '') {
			alert("请输入评论内容后再提交！")
		} else {
			$.ajax({
				url: ajax_url,
				data: "user_id=" + userId + "&pageindex=" + pageIndex + "&comment=" + comment + "&pageSize=" + 3,
				success: function(data) {
					alert("评论成功！");
					var a = eval("(" + data + ")");
					$(".tech-news-grids").empty();
					$.each(a, function(n, commentVal) {
						if (n == 0) {
							pageIndex = 1;
							pagesize = 3;
						}
						var result = "";
						result += "<div class='tech-news-grid span_66'>";
						result += commentVal.user_id;
						result += "<p>" + commentVal.content + "</p>";
						result += "</div>";
						$(".tech-news-grids").append(result);
					})
					$(".pageNum option[value=" + pageIndex + "]").attr("selected", true);
				},
				error: function(e) {
					alert("评论失败，请重新操作！");
				}
			})
		}
	})

})